package com.guigu.pd.work.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.utils.PageVo;
import com.guigu.pd.serviceapi.authfeign.CoreOrgFeign;
import com.guigu.pd.serviceapi.basefeign.DriverFeign;
import com.guigu.pd.serviceapi.basefeign.FleetFeign;
import com.guigu.pd.serviceapi.basefeign.TruckFeign;
import com.guigu.pd.serviceapi.authfeign.UserFeign;
import com.guigu.pd.work.entity.TransportOrderEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.common.utils.PageUtils;
import com.guigu.common.utils.Query;

import com.guigu.pd.work.dao.DriverJobDao;
import com.guigu.pd.work.entity.DriverJobEntity;
import com.guigu.pd.work.service.DriverJobService;


@Service("driverJobService")
public class DriverJobServiceImpl extends ServiceImpl<DriverJobDao, DriverJobEntity> implements DriverJobService {

    @Autowired(required = false)
    private DriverFeign driverFeign;

    @Autowired(required = false)
    private UserFeign userFeign;

    @Autowired(required = false)
    private FleetFeign fleetFeign;

    @Autowired(required = false)
    private TruckFeign truckFeign;

    @Autowired(required = false)
    private CoreOrgFeign coreOrgFeign;

    @Override
    public PageVo<DriverJobEntity> queryPage(Map<String, Object> params) {
        DriverJobEntity
                driverjobinfo = JSON.parseObject(JSON.toJSONString(params),
                DriverJobEntity.class);
        Integer pageno = Integer.parseInt(params.get("pageno").toString());
        Integer pagesize = Integer.parseInt(params.get("pagesize").toString());

        LambdaQueryWrapper<DriverJobEntity> queryWrapper = new LambdaQueryWrapper<DriverJobEntity>();
        queryWrapper.like(StringUtils.isNotEmpty(driverjobinfo.getId()),
                DriverJobEntity::getId, driverjobinfo.getId());

        if(StringUtils.isNotEmpty(driverjobinfo.getDivername())) {
            String uid = userFeign.getidbyName(driverjobinfo.getDivername());
            queryWrapper.like(StringUtils.isNotEmpty(uid),
                    DriverJobEntity::getDriverId, uid);
        }

        queryWrapper.like(driverjobinfo.getStatus()!=null&&driverjobinfo.getStatus()!=0,
                DriverJobEntity::getStatus, driverjobinfo.getStatus());

        queryWrapper.like(StringUtils.isNotEmpty(driverjobinfo.getTaskTransportId()),
                DriverJobEntity::getTaskTransportId, driverjobinfo.getTaskTransportId());


        PageHelper.startPage(pageno, pagesize);

        List<DriverJobEntity> driverJob = this.list(queryWrapper);

        for (DriverJobEntity driverjob : driverJob) {
            //获取司机信息
            String driverstr = driverFeign.getdriverbyId(driverjob.getDriverId());
            String[] driver = driverstr.split(",");
            String user = userFeign.getUser(driver[1]);
            //获取车队信息
            String agencyId = fleetFeign.getfleetbyId(driver[0]);
            String coreOrg = coreOrgFeign.getcoreorgbyId(agencyId);
            //获取车辆信息
            String truck = truckFeign.gettruckbyfid(driver[0]);
            driverjob.setDivername(user);
            driverjob.setCoreOrg(coreOrg);
            driverjob.setLicense(truck);
        }
        PageInfo<DriverJobEntity> pageInfo = new PageInfo<DriverJobEntity>(driverJob);

        PageVo<DriverJobEntity> pageVo = new PageVo<DriverJobEntity>();
        pageVo.setRows(pageInfo.getList());
        pageVo.setTotal(pageInfo.getTotal());

        return pageVo;
    }

}